package com.wty.shop.common;


import com.wty.shop.util.PropertiesUtil;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.util.Hashing;
import redis.clients.util.Sharded;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author wangtengyu
 * @Create 2018-01-30-14:48
 */
public class RedisShardedPool {
    //声明jedis连接池
    private static ShardedJedisPool pool;
    private static Integer maxTotal = Integer.parseInt(PropertiesUtil.getProperty("redis.max.total", "20"));
    private static Integer maxIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.max.idle", "10"));
    private static Integer minIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.min.idle", "2"));
    private static Boolean testOnBorrow = Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.borrow"));
    private static Boolean testOnReturn = Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.return"));

    private static String redis1Ip = PropertiesUtil.getProperty("redis1.ip");
    private static Integer redis1Port = Integer.parseInt(PropertiesUtil.getProperty("redis1.port"));
    private static String redis2Ip = PropertiesUtil.getProperty("redis2.ip");
    private static Integer redis2Port = Integer.parseInt(PropertiesUtil.getProperty("redis2.port"));



    //初始化连接池
    private static void initPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal);//设置最大连接数
        config.setMaxIdle(maxIdle);//最大连接数
        config.setMinIdle(minIdle);//最小连接数
        //生产环境为了性能 一般不进行验证操作
        config.setTestOnBorrow(testOnBorrow);//在borrow一个jedis实例的时候，是否要进行验证操作，如果赋值true。则得到的jedis实例肯定是可以用的。
        config.setTestOnReturn(testOnReturn);//在return一个jedis实例的时候，是否要进行验证操作，如果赋值true。则放回jedispool的jedis实例肯定是可以用的。

        config.setBlockWhenExhausted(true); //连接耗尽时，是否进入阻塞状态，false进入阻塞状态，true直至阻塞时尽

        JedisShardInfo jedisShardInfo1=new JedisShardInfo(redis1Ip,redis1Port,1000*2);
        JedisShardInfo jedisShardInfo2=new JedisShardInfo(redis2Ip,redis2Port,1000*2);
        //jedisShardInfo1.setPassword(); 设置密码
        List<JedisShardInfo> jedisShardInfoList=new ArrayList<JedisShardInfo>(2);//2是直接指定容量了且不会扩容，如果不指定，默认容量是10，每次扩容1.5倍
        jedisShardInfoList.add(jedisShardInfo1);
        jedisShardInfoList.add(jedisShardInfo2);
        //Hashing.MURMUR_HASH 一致性hash算法,
        pool = new ShardedJedisPool(config,jedisShardInfoList, Hashing.MURMUR_HASH, Sharded.DEFAULT_KEY_TAG_PATTERN);


    }

    //随着tomcat启动项目，随着类加载而调用初始化方法，进行连接池的初始化

    static {
        initPool();
    }

    //获得jedis实例
    public static ShardedJedis getJedis(){
        return pool.getResource();
    }

    //返回jedis资源
    public static void returnResource(ShardedJedis shardedJedis){
        pool.returnResource(shardedJedis);
    }
    //使用出错的归还
    public static void returnBorkenResource(ShardedJedis shardedJedis){
        pool.returnBrokenResource(shardedJedis);
    }


    public static void main(String[] args) {
        ShardedJedis jedis=pool.getResource();
        for(int i=0;i<10;i++){
            jedis.set("key"+i,"value"+i);
        }
        returnResource(jedis);
        System.out.println("program is end ");
    }
}
